package com.mall.db.api;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.SmsFlashPromotionMapper;
import com.mall.domain.SmsFlashPromotion;
import com.mall.interface1.api.SmsFlashPromotionApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsFlashPromotionApiImpl implements SmsFlashPromotionApi {

    @Autowired
    private SmsFlashPromotionMapper smsFlashPromotionMapper;

    //添加活动
    public void create(SmsFlashPromotion smsFlashPromotion) {
        smsFlashPromotionMapper.insert(smsFlashPromotion);
    }

    //删除活动
    public void delete(Long id) {
        smsFlashPromotionMapper.deleteById(id);
    }

    //获取活动分页数据
    public List<SmsFlashPromotion> list(String keyword, Integer pageNum, Integer pageSize) {
        Page<SmsFlashPromotion> pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsFlashPromotion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(keyword),SmsFlashPromotion::getTitle,keyword);
        Page<SmsFlashPromotion> promotionPage = smsFlashPromotionMapper.selectPage(pageInfo, queryWrapper);
        return promotionPage.getRecords();
    }

    //获取活动总数
    public Integer count(String keyword, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<SmsFlashPromotion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(keyword),SmsFlashPromotion::getTitle,keyword);
        return smsFlashPromotionMapper.selectCount(queryWrapper);
    }

    //根据id获取活动
    public SmsFlashPromotion findById(Long id) {
        return smsFlashPromotionMapper.selectById(id);
    }

    //修改线上线下状态
    public void updateStatus(SmsFlashPromotion smsFlashPromotion) {
        smsFlashPromotionMapper.updateById(smsFlashPromotion);
    }

    //编辑活动
    public void update(SmsFlashPromotion smsFlashPromotion) {
        smsFlashPromotionMapper.updateById(smsFlashPromotion);
    }


}
